<template>
  <div class="attributes-page">
    <el-card>
      <template #header>
        <div class="card-header">
          <span class="title">🏷️ 会员属性管理</span>
          <el-button type="primary" @click="handleAdd">
            <el-icon><Plus /></el-icon>
            新增属性
          </el-button>
        </div>
      </template>

      <el-table :data="tableData" border stripe>
        <el-table-column prop="name" label="属性名称" width="200" />
        <el-table-column prop="code" label="属性编码" width="150" />
        <el-table-column prop="type" label="属性类型" width="150">
          <template #default="{ row }">
            {{ getTypeName(row.type) }}
          </template>
        </el-table-column>
        <el-table-column prop="options" label="选项值" min-width="300">
          <template #default="{ row }">
            <el-tag v-for="opt in row.options" :key="opt" size="small" style="margin-right: 5px;">
              {{ opt }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="required" label="必填" width="80">
          <template #default="{ row }">
            <el-tag :type="row.required ? 'danger' : 'info'" size="small">
              {{ row.required ? '是' : '否' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="enabled" label="启用" width="80">
          <template #default="{ row }">
            <el-switch v-model="row.enabled" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="dialogVisible" title="会员属性配置" width="600px">
      <el-form :model="form" label-width="100px">
        <el-form-item label="属性名称">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="属性编码">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="属性类型">
          <el-select v-model="form.type">
            <el-option label="文本" value="text" />
            <el-option label="数字" value="number" />
            <el-option label="日期" value="date" />
            <el-option label="单选" value="radio" />
            <el-option label="多选" value="checkbox" />
          </el-select>
        </el-form-item>
        <el-form-item label="选项值" v-if="['radio', 'checkbox'].includes(form.type)">
          <el-input v-model="form.optionsText" placeholder="多个选项用逗号分隔" />
        </el-form-item>
        <el-form-item label="是否必填">
          <el-switch v-model="form.required" />
        </el-form-item>
        <el-form-item label="启用状态">
          <el-switch v-model="form.enabled" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

const tableData = ref([
  { name: '职业', code: 'occupation', type: 'radio', options: ['学生', '上班族', '自由职业', '其他'], required: false, enabled: true },
  { name: '兴趣爱好', code: 'hobbies', type: 'checkbox', options: ['运动', '阅读', '旅游', '美食'], required: false, enabled: true },
  { name: '收入水平', code: 'income', type: 'radio', options: ['3000以下', '3000-5000', '5000-10000', '10000以上'], required: false, enabled: true }
])

const dialogVisible = ref(false)
const form = reactive({
  name: '',
  code: '',
  type: 'text',
  optionsText: '',
  required: false,
  enabled: true
})

const getTypeName = (type) => {
  const map = { text: '文本', number: '数字', date: '日期', radio: '单选', checkbox: '多选' }
  return map[type] || type
}

const handleAdd = () => {
  dialogVisible.value = true
}

const handleEdit = (row) => {
  Object.assign(form, { ...row, optionsText: row.options?.join(',') || '' })
  dialogVisible.value = true
}

const handleDelete = () => {
  ElMessage.success('删除成功')
}

const handleSubmit = () => {
  ElMessage.success('保存成功')
  dialogVisible.value = false
}
</script>

<style scoped>
.attributes-page {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}
</style>

